﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain;
using Brain.Kinect;
using Microsoft.Xna.Framework.Graphics;
using Brain.Rendering;
using Microsoft.Xna.Framework;

namespace Kinect_Samples
{
    public class KinectDataScreen : GameScreen
    {
        public KinectManager Manager;

        public Sprite ColorSprite;
        public Sprite DepthSprite;
        public Sprite SkeletonSprite;

        private KinectSkeletonDrawer SkeletonDrawer;
        public KinectDataScreen()
        {
            this.Manager = Engine.Instance.GetManager<KinectManager>();
            Manager.ReadFaceTracking = true;

            ColorSprite = new Sprite();
            ColorSprite.Rendering = SpriteRendering.Rendered;
            ColorSprite.Effect = Engine.Instance.ContentManager.Load<Effect>("KinectColor");

            DepthSprite = new Sprite();
            DepthSprite.Rendering = SpriteRendering.Rendered;
            DepthSprite.Effect = Engine.Instance.ContentManager.Load<Effect>("KinectDepth");

            SkeletonSprite = new Sprite();
            SkeletonSprite.Rendering = SpriteRendering.Rendered;

            Texture2D Joint = Engine.Instance.ContentManager.Load<Texture2D>("Joint");
            SkeletonDrawer = new KinectSkeletonDrawer(this.Manager,
                new SpriteBatch(Engine.Instance.GraphicsDevice), Joint, Vector2.Zero,
                KinectUtil.SkeletonToColorMap);

            DefaultRenderManager2D renderManager = Engine.Instance.RenderManager2D as DefaultRenderManager2D;
            renderManager.BackBufferColor = Color.Black;
        }

        public override void Update()
        {
            base.Update();

            if (this.Manager.Sensor != null)
            {
                ColorSprite.Texture = this.Manager.GetColorTexture();
                ColorSprite.BoundsRectangle = new Rectangle(960, 50, 320, 240);

                SkeletonSprite.Texture = SkeletonDrawer.SkeletonTarget;
                SkeletonSprite.BoundsRectangle = new Rectangle(960, 50, 320, 240);

                DepthSprite.Texture = this.Manager.GetDepthTexture();
                DepthSprite.BoundsRectangle = new Rectangle(960, 400, 320, 240);
            }
        }

        public override void Draw()
        {
            SkeletonDrawer.Draw();

            base.Draw();
        }
    }
}
